﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace com.yd.sdk
{
    public class AnalysisSetting : ISetting
    {
        private bool _isFacebook = false;
        private bool _isAppsFlyer = false;
        private bool _isAdjust = false;
        
        private bool _isFirebaseAnalytics = false;
        private bool _isPerformanceMonitoring = false;
        private bool _isCrashlytics = false;

        public void Init()
        {
            _isFirebaseAnalytics = SDKEditorWindow.sdkSettings.isOpen_Firebase;
            _isPerformanceMonitoring = SDKEditorWindow.sdkSettings.isOpen_Performance_Monitoring;
            _isCrashlytics = SDKEditorWindow.sdkSettings.isOpen_Crashlytics;
            _isAdjust = SDKEditorWindow.sdkSettings.isOpen_Adjust;
        }

        public void OnGUI()
        {
            GUILayout.Label("数据分析SDK模块");
            GUILayout.BeginHorizontal();
            _isAdjust = GUILayout.Toggle(_isAdjust, "Adjust");
            _isFacebook = GUILayout.Toggle(_isFacebook, "facebook-测试");
            _isAppsFlyer = GUILayout.Toggle(_isAppsFlyer, "AppsFlyer-测试");
           
            GUILayout.EndHorizontal();
            
            GUILayout.Label("性能分析SDK模块");
            GUILayout.BeginHorizontal();
            _isFirebaseAnalytics = GUILayout.Toggle(_isFirebaseAnalytics, "Firebase-Analytics");
            _isPerformanceMonitoring = GUILayout.Toggle(_isPerformanceMonitoring, "Firebase-PerformanceMonitoring");
            _isCrashlytics = GUILayout.Toggle(_isCrashlytics, "Firebase-Crashlytics");
            GUILayout.EndHorizontal();
        }

        public void Apply()
        {
            if (_isFirebaseAnalytics)
                InstallFirebaseAnalysics();
            else
                RemoveFirebaseAnalysics();

            if (_isCrashlytics)
                InstallCrashlytics();
            else
                RemoveFirebaseCrashlytics();

            if (_isPerformanceMonitoring)
                InstallPerformanceMonitoring();
            else
                RemovePerformanceMonitoring();
            

            // if (_isFacebook)
            //     InstallFacebook();
            // else
            //     RemoveFacebook();
            //
            // if (_isAppsFlyer)
            //     InstallAppsFlyer();

            if (_isAdjust)
                InstallAdjust();
            else
                RemoveAdjust();

            SDKEditorWindow.sdkSettings.isOpen_Firebase = _isFirebaseAnalytics;
            SDKEditorWindow.sdkSettings.isOpen_Performance_Monitoring = _isPerformanceMonitoring;
            SDKEditorWindow.sdkSettings.isOpen_Crashlytics = _isCrashlytics;
            SDKEditorWindow.sdkSettings.isOpen_Adjust = _isAdjust;

        }
        

        public bool IsSelectedSomething()
        {
            return _isFirebaseAnalytics || _isCrashlytics || _isPerformanceMonitoring || _isFacebook || _isAppsFlyer ||
                   _isAdjust;
        }

        private void InstallCrashlytics()
        {
            //向Gradle中添加依赖库
            Utils.MergeDependenciesInGradle(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                Constants.FIREBASE_CRASHLYTICS_DEPENDENCIES);
            
            //在添加class
            InsertDependencies(Constants.GetFullPath(Constants.ANDROID_BASE_TEMPLATE_PATH),
                Constants.FIREBASE_BASE_PROJECT_dependencies);
            
            //复制Launcher
            if (!File.Exists(Constants.GetFullPath(Constants.ANDROID_LAUNCHER_TEMPLATE_PATH)))
                FileUtil.ReplaceFile(Constants.GetFullPath(Constants.ORIGIN_LAUNCHER_TEMPLATE,true), Constants.GetFullPath(Constants.ANDROID_LAUNCHER_TEMPLATE_PATH));
            
            //加入插件关联
            InsertPlugins(Constants.GetFullPath(Constants.ANDROID_LAUNCHER_TEMPLATE_PATH),Constants.FIREBASE_LAUNCHER_PLUGINS);
            Debug.Log("<color=#00FF00>成功添加Crashlytics</color>");
        }

        private void RemoveFirebaseCrashlytics()
        {
            Utils.DeleteStringFromFile(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                "\t"+Constants.FIREBASE_CRASHLYTICS_DEPENDENCIES +"\r");
            
            Utils.DeleteStringFromFile(Constants.GetFullPath(Constants.ANDROID_BASE_TEMPLATE_PATH),
                "\t"+Constants.FIREBASE_BASE_PROJECT_dependencies +"\r");
            
            Utils.DeleteStringFromFile(Constants.GetFullPath(Constants.ANDROID_LAUNCHER_TEMPLATE_PATH),
                Constants.FIREBASE_LAUNCHER_PLUGINS +"\r");
        }
        
        private void InstallPerformanceMonitoring()
        {
            //向Gradle中添加依赖库
            Utils.MergeDependenciesInGradle(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                Constants.FIREBASE_PERF_DEPENDENCIES);
            
            //在添加class
            InsertDependencies(Constants.GetFullPath(Constants.ANDROID_BASE_TEMPLATE_PATH),
                Constants.FIREBASE_PERF_BASE_PROJECT_dependencies);
            
            //复制Launcher
            if (!File.Exists(Constants.GetFullPath(Constants.ANDROID_LAUNCHER_TEMPLATE_PATH)))
                FileUtil.ReplaceFile(Constants.GetFullPath(Constants.ORIGIN_LAUNCHER_TEMPLATE,true), Constants.GetFullPath(Constants.ANDROID_LAUNCHER_TEMPLATE_PATH));
            
            //加入插件关联
            InsertPlugins(Constants.GetFullPath(Constants.ANDROID_LAUNCHER_TEMPLATE_PATH),Constants.FIREBASE_PERF_LAUNCHER_PLUGINS);
            Debug.Log("<color=#00FF00>成功添加PerformanceMonitoring</color>");
        }
        
        private void RemovePerformanceMonitoring()
        {
            Utils.DeleteStringFromFile(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                "\t"+Constants.FIREBASE_PERF_DEPENDENCIES +"\r");
            
            Utils.DeleteStringFromFile(Constants.GetFullPath(Constants.ANDROID_BASE_TEMPLATE_PATH),
                "\t"+Constants.FIREBASE_PERF_BASE_PROJECT_dependencies +"\r");
            
            Utils.DeleteStringFromFile(Constants.GetFullPath(Constants.ANDROID_LAUNCHER_TEMPLATE_PATH),
                Constants.FIREBASE_PERF_LAUNCHER_PLUGINS +"\r");
        }
        
        private void InstallAdjust()
        {
            //向Gradle中添加依赖库
            Utils.MergeDependenciesInGradle(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                Constants.ADJUST_DEPENDENCIES);
            
            //添加权限
            Utils.MergeAndroidManifest(Constants.GetFullPath(Constants.ADJUST_MANIFEST, true),
                Constants.GetFullPath(Constants.ANDROID_MAIN_MANIFEST));
            
            //实现库
            Utils.MergeDependenciesInGradle(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                Constants.ADJUST_IMP_DEPENDENCIES);
            
        }
        
        private void RemoveAdjust()
        {
            Utils.DeleteStringFromFile(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                "\t"+Constants.ADJUST_DEPENDENCIES +"\r");
            
            Utils.DeleteStringFromFile(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                "\t"+Constants.ADJUST_IMP_DEPENDENCIES +"\r");
        }


        private void InstallFacebook()
        {
            //向Gradle中添加依赖库
            Utils.MergeDependenciesInGradle(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                Constants.FACEBOOK_DEPENDENCIES);
            
            //添加权限
            Utils.MergeAndroidManifest(Constants.GetFullPath(Constants.FACEBOOK_MANIFEST, true),
                Constants.GetFullPath(Constants.ANDROID_MAIN_MANIFEST));
            
            //实现库
            Utils.MergeDependenciesInGradle(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                Constants.FACEBOOK_IMP_DEPENDENCIES);

            //复制string中的ApplicationId,用直接写的还不行
            Utils.MergeAndroidValuesString(Constants.GetFullPath(Constants.FACEBOOK_RES_VALUES_STRING, true),
                Constants.GetFullPath(Constants.ANDROID_PLUGINS_RES_VALUE_PATH + "strings.xml"));
        }

        private void InstallAppsFlyer()
        {
            //向Gradle中添加依赖库
            Utils.MergeDependenciesInGradle(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                Constants.APPSFLYER_DEPENDENCIES);
            
            //添加权限
            Utils.MergeAndroidManifest(Constants.GetFullPath(Constants.APPSFLYER_MANIFEST, true),
                Constants.GetFullPath(Constants.ANDROID_MAIN_MANIFEST));
            
            //实现库
            Utils.MergeDependenciesInGradle(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                Constants.APPSFLYER_IMP_DEPENDENCIES);
        }

        private void InstallFirebaseAnalysics()
        {
            //向Gradle中添加依赖库
            Utils.MergeDependenciesInGradle(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                Constants.FIREBASE_DEPENDENCIES);
            
            //添加权限
            Utils.MergeAndroidManifest(Constants.GetFullPath(Constants.FIREBASE_MANIFEST, true),
                Constants.GetFullPath(Constants.ANDROID_MAIN_MANIFEST));
            
            //实现库
            Utils.MergeDependenciesInGradle(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                Constants.FIREBASE_IMP_DEPENDENCIES);
            
            //复制string中的ApplicationId,用直接写的还不行
            Utils.CopyAndroidValues(Constants.GetFullPath(Constants.FIREBASE_GOOGLE_PLAY_SERIVCES, true),
                Constants.GetFullPath(Constants.ANDROID_PLUGINS_RES_VALUE_PATH + "google-services.xml"));

            Utils.ReplaceGoogleService(Constants.GetFullPath(Constants.ANDROID_PLUGINS_RES_VALUE_PATH + "google-services.xml"));
            Debug.Log("<color=#00FF00>成功添加FirebaseAnalysics</color>");
        }

        private void RemoveFirebaseAnalysics()
        {
            //会保留下权限或者meta-data
            Utils.DeleteStringFromFile(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                "\t"+Constants.FIREBASE_DEPENDENCIES + "\r");
            Utils.DeleteStringFromFile(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                "\t"+Constants.FIREBASE_IMP_DEPENDENCIES + "\r");
            var dest = Constants.GetFullPath(Constants.ANDROID_PLUGINS_RES_VALUE_PATH + "google-services.xml");
            if(File.Exists(dest))
                File.Delete(dest);
            
        }

        public static void InsertDependencies(string source,string classpath)
        {
            if (!File.Exists(source)) return;
            var text = File.ReadAllText(source);
            if (text.Contains(classpath) == false)
            {
                var index = text.IndexOf("**BUILD_SCRIPT_DEPS**");
                if (index != -1)
                {
                    var insert = text.Insert(index, "\t"+classpath+"\r");
                    File.WriteAllText(source,insert);
                }
            }
        }

        public static void InsertPlugins(string source, string plugin)
        {
            if (!File.Exists(source)) return;
            var text = File.ReadAllText(source);
            if (text.Contains(plugin) == false)
            {
                var index = text.IndexOf("dependencies");
                if (index != -1)
                {
                    var insert = text.Insert(index, plugin+"\r");
                    File.WriteAllText(source,insert);
                }
            }
            
            
        }


        
    }
}